﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BanVeXemPhim_Presentation
{
    public partial class UserControlRapChieu : UserControl
    {
        public UserControlRapChieu()
        {
            InitializeComponent();
        }

        private UserControlGheNgoi[] ucGhe;
        private GheDTO[] _dsGhe;
        public GheDTO[] DsGhe
        {
            get { return _dsGhe; }
            set
            {
                _dsGhe = value;
                if (_dsGhe != null)
                {
                    if(ucGhe!=null)
                    {
                        if(ucGhe.Length>0)
                        {
                            foreach (UserControlGheNgoi var in ucGhe)
                            {
                                var.Dispose();
                            }
                        }

                    }
                    KhoiTaoDanhSachGhe();
                }
                
            }
        }
        private void UserControlRapChieu_Load(object sender, EventArgs e)
        {            
        }
        private void KhoiTaoDanhSachGhe()
        {
            if (_dsGhe.Length > 0)
            {
                ucGhe=new UserControlGheNgoi[_dsGhe.Length];
                for(int i=0;i<DsGhe.Length;i++)
                {
                    ucGhe[i] = new UserControlGheNgoi();
                    ucGhe[i].Cot = DsGhe[i].Cot;
                    ucGhe[i].Hang = DsGhe[i].Hang;
                    ucGhe[i].IdGhe = DsGhe[i].IdGhe;
                    ucGhe[i].Location = new System.Drawing.Point(25 * DsGhe[i].Cot + 23, 25 * DsGhe[i].Hang + 73);
                    ucGhe[i].Size = new System.Drawing.Size(25, 25);
                    ucGhe[i].TabIndex = 1;
                    ucGhe[i].TinhTrang = GheState.CHUACHON;
                    this.Controls.Add(ucGhe[i]);
                }
            }
        }

        public void CapNhatTinhTrangGheDaDat(GheDTO[] dsghe)
        {
            int n = dsghe.Length;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<ucGhe.Length;j++)
                {
                    if(ucGhe[j].Hang==dsghe[i].Hang && ucGhe[j].Cot==dsghe[i].Cot)
                    {
                        ucGhe[j].TinhTrang = GheState.DACHON;
                        break;
                    }
                }
            }
        }

        public GheDTO[] DanhSachGheDuocChon()
        {
            int n = ucGhe.Length;
            List<GheDTO> dsGhe=new List<GheDTO>();
            for (int i = 0; i < n;i++ )
            {
                if (ucGhe[i].TinhTrang == GheState.DANGCHON)
                {
                    GheDTO ghe=new GheDTO();
                    ghe.Cot = ucGhe[i].Cot;
                    ghe.Hang = ucGhe[i].Hang;
                    ghe.IdGhe = ucGhe[i].IdGhe;
                    dsGhe.Add(ghe);
                }
            }
            return dsGhe.ToArray();
        }


    }
}
